#if 0
echo "Please enter the password"
sudo ls >/dev/null; sudo ls >/dev/null; sudo ls >/dev/null
sudo apt install -y g++ libglfw3-dev
g++ -x c++ $0 -o opengl_demo -lglfw -lGL -lX11 -lpthread -lXrandr
./opengl_demo; rm -rf opengl_demo
exit
#endif

#include <GLFW/glfw3.h>
#include <iostream>
inline long long int timestamp_us(void);

int main() {
    // 窗口大小
    const int WIDTH = 800;
    const int HEIGHT = 600;

    // 初始化GLFW
    if (!glfwInit()) {
        std::cerr << "GLFW初始化失败" << std::endl;
        return -1;
    }

    // 创建窗口
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL Demo", nullptr, nullptr);
    if (!window) {
        std::cerr << "窗口创建失败" << std::endl;
        glfwTerminate();
        return -1;
    }

    // 设置当前上下文
    glfwMakeContextCurrent(window);

    // 主循环
    while (!glfwWindowShouldClose(window)) {
        // 清空颜色缓冲区
        glClear(GL_COLOR_BUFFER_BIT);
        
        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();        

        // 交换前后缓冲区
        glfwSwapBuffers(window);

        // 处理事件
        glfwPollEvents();
		
		static auto time0 = timestamp_us();
		if(timestamp_us() - time0 > 1000000 * 5) break;
    }

    // 终止GLFW
    glfwTerminate();
    return 0;
}

#include <time.h>
inline long long int timestamp_us(void) {
  struct timespec tp;
  clock_gettime(CLOCK_MONOTONIC, &tp);
  long long int time0 = (long long int)tp.tv_sec * 1000000;
  long long int time1 = (long long int)tp.tv_nsec / 1000;
  return time0 + time1;
}